package com.readytocloud.r2cSoonSite;

import java.io.IOException;
import java.util.Date;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import com.readytocloud.r2cSoonSite.SmallNote;
import com.readytocloud.r2cSoonSite.PMF;

@SuppressWarnings("serial")
public class LeaveNoteServlet extends HttpServlet {
	
	private static final Logger log = Logger.getLogger(LeaveNoteServlet.class.getName());
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
				throws IOException {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		
		String content = req.getParameter("content");
		Date date = new Date();
		
		SmallNote note = new SmallNote(user,content,date);
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			log.info("A User just added a new note.");
			pm.makePersistent(note);
		}
		finally {
			pm.close();
		}
		resp.sendRedirect("/index.jsp");
	}
}
